'add user sales channle in C1 for bulk users
'author: chaoyuew@cn.ibm.com
'updated on 01/03/16 prototype
'updated on 29/07/16 add code to  filter when the  ID doesn't exist or has multiple matched value

ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
winstr = "https://xxx/uiPage"
Set tempwins=createobject("shell.application") 
Set winsWindows = tempwins.Windows
dim IE,URL
'<-----------get ie object'
URL="https://xxx/cis.jsp"
For Each WebBrowser In winsWindows
	If WebBrowser.visible =true then
		If Instr(WebBrowser.LocationURL,URL) =1 Then
			Set ie=WebBrowser
			exit for
		end if
	end if
next
Set tempwins =nothing : Set winsWindows =nothing
'<-----------------------------get bulk user list
CkUserIDTxt
dim descheList(1) : descheList(0)="CIC_SAL" : descheList(1)= "CIC_SRV"
const C_Type = "SALCHN"

for each user in UserIDTxt
	'<---------------------Search end user'
	validUser(user)
	if isErrorIDFound = "false"  then
		'<--------------get sales part'
		Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
		Set userCharGridContent = TabPageContent.document.getelementbyid("USER_CHAR_GRID").contentWindow
		Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
		on error resume next
		while Characteristics Is Nothing
			WScript.Sleep 200 : Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
		wend
		on error goto 0		
		'<-----------------------------get end user sals changgel list'
		dim EndUserSalesList : EndUserSalesList=";"
		for each item in Characteristics
			if right(item.id,9) = "$CHAR_VAL" and len(item.value) > 2 and item.value <> "CICLVL12" then
				EndUserSalesList = item.value & ";" & EndUserSalesList
			end if
		Next
		'<------------------------Add end user sale channel'
		dim seqnum : seqnum = 1
		for each item in descheList
			Set regEx = New RegExp
			str=item & ";" : regEx.Pattern = str
			regEx.Global = True : res = regEx.Test(EndUserSalesList)
			if res = "False" then			
				Set Characteristics= userCharGridContent.document.getelementsbytagname("input")
				dim loopnum
				for each inputs in Characteristics
					if inputs.value <> "" then
						loopnum=1 : charAddID = "IM_USER_CHAR:0$ADD"
						on error resume next '异常处理'
						Set charAdd =userCharGridContent.document.getelementbyid(charAddID)
						while err.number <> 0 
							Err.Clear : Set charAdd =userCharGridContent.document.getelementbyid(charAddID)
						wend
						on error  goto 0
						charAdd.click : WScript.Sleep 2000
						Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
						exit for
					else
						loopnum=0 : exit for
					end if
				next
				charTypeID ="USER_CHAR:" & loopnum & "$CHAR_TYPE_CD"
				SeqID ="USER_CHAR:" & loopnum & "$SEQ_NUM"
				charValueID ="USER_CHAR:" & loopnum & "$CHAR_VAL"
				
				Set charType = userCharGridContent.document.getelementbyid(charTypeID)
				Set Seq = userCharGridContent.document.getelementbyid(SeqID)
				Set charValue = userCharGridContent.document.getelementbyid(charValueID)
				
				charType.focus : charType.value=C_Type : charType.FireEvent("onchange")
				WScript.Sleep 1500 : Seq.focus : Seq.value=seqnum : Seq.FireEvent("onblur")
				charValue.focus : charValue.value=item : charValue.FireEvent("onblur") : WScript.Sleep 1500
			end if
			seqnum=seqnum+1
		next
		EndUserSalesList =";"
		ie.document.frames(0).document.getelementbyid("IM_SAVE").click
		WScript.Sleep 5000
	end if
next
msgbox "Script finished successfully!"
